/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package eliminar;

import apunte.Carrera;
import apunte.ManejoApunte;
import apunte.Materia;
import crear.crearCarrera;
import crear.crearMateria;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author martinLequerica
 */
public class eliminarMateria {

    private LinkedList<Materia> llm;
    private java.awt.List list1;
    private javax.swing.JScrollPane jScrollPane1;
    private VistaEliminar ve;
    private java.awt.List lista;
    private JButton botonMas, botonMenos, refresh;
    private ManejoApunte ma;

    public eliminarMateria() throws SQLException {
        ve = new VistaEliminar();
        
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        ve.setLocation(d.width/2,d.height/2);
        
        
        ma = new ManejoApunte();
        botonMas = ve.getBotonMas();
        botonMenos = ve.getBotonMenos();
        refresh = ve.getRefresh();
        lista = ve.getLista();
        llm = new LinkedList();


        cargarLinkedList();
        cargarLista(llm);


        botonMas.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {

                new crearMateria();

            }
        });

        botonMenos.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                try {
                    boolean verDep = ma.verificoDependenciasMateria(llm.get(lista.getSelectedIndex()));
                    if (verDep == false){
                        
                        ma.eliminarMateria(llm.get(lista.getSelectedIndex()));
                        try {
                    cargarLinkedList();
                } catch (SQLException ex) {
                    Logger.getLogger(eliminarMateria.class.getName()).log(Level.SEVERE, null, ex);
                }
                cargarLista(llm);
                        
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(eliminarMateria.class.getName()).log(Level.SEVERE, null, ex);
                }
                
                    
        

            }
        });

        refresh.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                try {
                    cargarLinkedList();
                } catch (SQLException ex) {
                    Logger.getLogger(eliminarMateria.class.getName()).log(Level.SEVERE, null, ex);
                }
                cargarLista(llm);


            }
        });


        ve.setVisible(true);

    }

    private void cargarLinkedList() throws SQLException {
        llm.clear();
        llm = ma.traerMaterias();
    }

    private void cargarLista(LinkedList<Materia> llm) {
        lista.removeAll();

        Iterator<Materia> i = llm.iterator();
        while (i.hasNext()) {
            Materia next = i.next();

            lista.add(next.getNombreMateria());

        }

    }
}
